/*     */ package com.gopay.bis.gdb.wap.gfbank.payment.merchant;
/*     */ 
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.ObjectInputStream;
/*     */ import java.io.ObjectOutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.security.KeyPair;
/*     */ import java.security.KeyPairGenerator;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.PublicKey;
/*     */ import java.security.SecureRandom;
/*     */ 
/*     */ class KeyManager
/*     */ {
/*     */   public static void createKeyPair(String keyname)
/*     */     throws Exception
/*     */   {
/*     */     try
/*     */     {
/*  14 */       KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
/*  15 */       keyGen.initialize(1024, new SecureRandom());
/*  16 */       KeyPair pair = keyGen.generateKeyPair();
/*  17 */       PrivateKey priv = pair.getPrivate();
/*  18 */       PublicKey pub = pair.getPublic();
/*  19 */       FileOutputStream ostream = new FileOutputStream(keyname + ".private");
/*  20 */       ObjectOutputStream p = new ObjectOutputStream(ostream);
/*  21 */       p.writeObject(priv);
/*  22 */       p.close();
/*  23 */       ostream = new FileOutputStream(keyname + ".public");
/*  24 */       p = new ObjectOutputStream(ostream);
/*  25 */       p.writeObject(pub);
/*  26 */       p.close();
/*  27 */       System.out.println(priv.getAlgorithm());
/*  28 */       System.out.println(priv.getFormat());
/*  29 */       ostream.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  33 */       System.err.println("生成密匙错误：" + e.toString());
/*  34 */       throw e;
/*     */     }
/*     */   }
/*     */ 
/*     */   public static KeyPair getKeyPair(String keyname)
/*     */   {
/*     */     try
/*     */     {
/*  42 */       FileInputStream istream = new FileInputStream(keyname + ".private");
/*  43 */       ObjectInputStream p = new ObjectInputStream(istream);
/*  44 */       PrivateKey priv = (PrivateKey)p.readObject();
/*  45 */       p.close();
/*  46 */       istream = new FileInputStream(keyname + ".public");
/*  47 */       p = new ObjectInputStream(istream);
/*  48 */       PublicKey pub = (PublicKey)p.readObject();
/*  49 */       p.close();
/*  50 */       KeyPair pair = new KeyPair(pub, priv);
/*  51 */       return pair;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  55 */       System.err.println("in KeyManager.getKeyPair() Caught exception " + e.toString());
/*     */     }
/*  57 */     return null;
/*     */   }
/*     */ 
/*     */   public static PrivateKey getPrivateKey(String keyname)
/*     */   {
/*     */     try
/*     */     {
/*  64 */       FileInputStream istream = new FileInputStream(keyname + ".private");
/*  65 */       ObjectInputStream p = new ObjectInputStream(istream);
/*  66 */       PrivateKey priv = (PrivateKey)p.readObject();
/*  67 */       p.close();
/*  68 */       return priv;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  72 */       e.printStackTrace();
/*  73 */       System.err.println("in KeyManager.getPrivateKey()  Caught exception " + e.toString());
/*     */     }
/*  75 */     return null;
/*     */   }
/*     */ 
/*     */   public static PublicKey getPublicKey(String keyname)
/*     */   {
/*     */     try
/*     */     {
/*  82 */       FileInputStream istream = new FileInputStream(keyname + ".public");
/*  83 */       ObjectInputStream p = new ObjectInputStream(istream);
/*  84 */       PublicKey pub = (PublicKey)p.readObject();
/*  85 */       p.close();
/*  86 */       return pub;
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  90 */       System.err.println("in KeyManager.getPublicKey()  Caught exception " + e.toString());
/*     */     }
/*  92 */     return null;
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/*  97 */     KeyManager keyManager = new KeyManager();
/*  98 */     System.err.println("开始生成密匙对....");
/*  99 */     String keyaddr = "";
/*     */     try
/*     */     {
/* 102 */       keyaddr = PayCfg.getValue("merchid");
/* 103 */       createKeyPair("merkey" + keyaddr);
/*     */     }
/*     */     catch (Exception _ex)
/*     */     {
/* 107 */       System.err.println("请检查出错原因后再试");
/* 108 */       System.exit(1);
/*     */     }
/* 110 */     KeyPair pair = getKeyPair("merkey" + keyaddr);
/* 111 */     System.err.println(pair);
/* 112 */     System.err.println("生成密匙对成功");
/*     */   }
/*     */ }

/* Location:           D:\桌面\广发银行\B2C支付签名验签工具包\JAVA版\gfgate\WEB-INF\classes\
 * Qualified Name:     gfbank.payment.merchant.KeyManager
 * JD-Core Version:    0.6.0
 */